package br.com.escolaespacocrianca.fineec.controller;

import static br.com.caelum.vraptor.view.Results.json;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.validator.I18nMessage;
import br.com.caelum.vraptor.validator.Validations;
import br.com.escolaespacocrianca.fineec.business.EnrollmentBusiness;
import br.com.escolaespacocrianca.fineec.business.StudentBusiness;
import br.com.escolaespacocrianca.fineec.component.Resources;
import br.com.escolaespacocrianca.fineec.model.Enrollment;
import br.com.escolaespacocrianca.fineec.model.KinshipDegree;
import br.com.escolaespacocrianca.fineec.model.Student;
import br.com.escolaespacocrianca.fineec.model.enums.AddressType;
import br.com.escolaespacocrianca.fineec.model.enums.GenderType;
import br.com.escolaespacocrianca.fineec.model.enums.GradeType;
import br.com.escolaespacocrianca.fineec.ui.AutoCompleteResult;

@Resource
public class StudentController {

	private final Result result;
	private final StudentBusiness business;
	private final Validator validator;
	private final EnrollmentBusiness enrollmentBusiness;

	public StudentController(Result result,Validator validator, Resources resources, HttpServletRequest request , StudentBusiness business, EnrollmentBusiness enrollmentBusiness) {
		this.result = result;
		this.business = business;
		this.validator = validator;
		this.enrollmentBusiness = enrollmentBusiness;
	}
	
	@Get("/student")
	public void index() {
		result.forwardTo(SharedController.class).index("student","Aluno");
	}
 
	public List<Student> list() {

		return business.list();
	}
	
	public Student view(Student student) {
		
		Student studentFull = null;
		if (student != null && student.getId() != null){
			studentFull = business.find(student.getId());
		}
		return studentFull;
	}
	
	public Student maintain(Student student) {
		result.include("addressTypes", AddressType.values());
		result.include("genderTypes", GenderType.values());
		result.include("kinshipList", KinshipDegree.values());
		
		Student studentFull = null;
		if (student != null && student.getId() != null){
			studentFull = business.find(student.getId());
		}
		return studentFull;
	}

	public void delete(Student student) {

		business.delete(student);

		result.use(json()).withoutRoot().from(Resources.getMessage("student.delete.success")).serialize();
	}

	public void save(final Student student) {
		
		validator.checking(new Validations(){{
			that(!student.getName().isEmpty(), "validation","student.name.empty");
			that(student.getGender() != null, "validation","student.gender.empty");
			that(student.getBirthday() != null, "validation","student.birthday.empty");
			that(!student.getNacionality().isEmpty(), "validation","student.nacionality.empty");
			that(!student.getCitizenship().isEmpty(), "validation","student.citizenship.empty");
			that(student.getAddress() != null, "validation","student.address.empty");
			that(student.getAddress().getType() != null, "validation","student.address.type.empty");
			that(!student.getAddress().getName().isEmpty(), "validation","student.address.name.empty");
			that(!student.getAddress().getNumber().isEmpty(), "validation","student.address.number.empty");
			that(!student.getAddress().getNeighborhood().isEmpty(), "validation","student.address.neighborhood.empty");
			that(!student.getAddress().getCep().isEmpty(), "validation","student.address.cep.empty");
			that(!student.getAddress().getCity().isEmpty(), "validation","student.address.city.empty");
			that(student.getFirstSponsor().getId() != null, "validation","student.first.sponsor.empty");
			that(student.getSecondSponsor().getId() != null, "validation","student.second.sponsor.empty");
			that(student.getFinancialSponsor().getId() != null, "validation","student.financial.sponsor.empty");
	    }});  
		
		if (student.getEnrollmentNumber() != null && business.existStudentWithNumber(student.getEnrollmentNumber())) {  
			validator.add(new I18nMessage("validation", "student.duplicatedEnrollmentNumber"));  
		}
		
		if (business.existStudentWithName(student.getName())) {  
			validator.add(new I18nMessage("validation", "student.duplicatedStudentName"));  
		}  
		
		validator.onErrorSendBadRequest();
		
		business.saveOrUpdate(student);
		result.use(json()).withoutRoot().from(Resources.getMessage("student.save.success")).serialize();
	}
	
	public void autocomplete(String q){
		
		List<Student> students = business.searchByName(q);
		
		List<AutoCompleteResult> list = new ArrayList<AutoCompleteResult>();
		
		for (Student student : students) {
			AutoCompleteResult autoCompleteResult = new AutoCompleteResult();
			autoCompleteResult.setKey(student.getId().toString());
			autoCompleteResult.setText(student.getName());
			
			list.add(autoCompleteResult);
		}

		result.use(json()).withoutRoot().from(list).serialize();
		
	}
	
	public List<Student> listByGrade() {

		return business.list();
	}
	
	public List<Enrollment> listByGradeSearch(GradeType grade) {
		return enrollmentBusiness.listByGrade(grade);
	}
	
	public List<Student> listByGradeAll() {
		
		result.include("enrollmentListInfantOne", enrollmentBusiness.listByGrade(GradeType.INFANT_I));
		result.include("enrollmentListInfantTwo", enrollmentBusiness.listByGrade(GradeType.INFANT_II));
		result.include("enrollmentListInfantThree", enrollmentBusiness.listByGrade(GradeType.INFANT_III));
		result.include("enrollmentListInfantFour", enrollmentBusiness.listByGrade(GradeType.INFANT_IV));
		result.include("enrollmentListInfantFundamental", enrollmentBusiness.listByGrade(GradeType.FUNDAMENTAL));
		result.include("enrollmentListInfantNursery", enrollmentBusiness.listByGrade(GradeType.NURSERY));
		result.include("enrollmentListInfantNest", enrollmentBusiness.listByGrade(GradeType.NEST));
		
		return business.list();
	}
	
	public List<Enrollment> listByGradeInfantOne() {

		return result.forwardTo(this).listByGradeSearch(GradeType.INFANT_I);
	}
	
	public List<Enrollment> listByGradeInfantTwo() {
		return result.forwardTo(this).listByGradeSearch(GradeType.INFANT_II);
	}
	
	public List<Enrollment> listByGradeInfantThree() {
		return result.forwardTo(this).listByGradeSearch(GradeType.INFANT_III);
	}
	
	public List<Enrollment> listByGradeInfantFour() {
		return result.forwardTo(this).listByGradeSearch(GradeType.INFANT_IV);
	}
	
	public List<Enrollment> listByGradeNursery() {
		return result.forwardTo(this).listByGradeSearch(GradeType.NURSERY);
	}
	
	public List<Enrollment> listByGradeNest() {
		return result.forwardTo(this).listByGradeSearch(GradeType.NEST);
	}
	
	public List<Enrollment> listByGradeFundamental() {
		return result.forwardTo(this).listByGradeSearch(GradeType.FUNDAMENTAL);
	}
}
